package Offer.offer;

import java.util.Stack;

/**
 * 包含min函数的栈
 *
 * @author leiwenkang
 * @date 2020/4/6
 */
public class Test21包含min函数的栈 {

    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();

    public void push(int value) {
        stack1.push(value);
        if (stack2.isEmpty() || value < stack2.peek()) {
            stack2.push(value);
        } else {
            stack2.push(stack2.peek());
        }
    }

    public int pop() {
        if (stack1.isEmpty()) {
            throw new RuntimeException("为空");
        }
        stack2.pop();
        return stack1.pop();
    }

    public int min() {
        if (stack1.isEmpty()) {
            throw new RuntimeException("为空");
        }
        return stack2.peek();
    }

}
